﻿using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Jayden.Dll.Core.IO;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Web.Json;

namespace Jayden.Dll.Web.Controls
{

	public class WiiGameInfo
	{
		public string Code;
		public string Name;
		public double Length;
		public string Region = "EN";

		public bool Match(Regex regex, string text)
		{
			if (regex != null)
			{
				if (regex.IsMatch(Code))
					return true;
				if (regex.IsMatch(Name))
					return true;
			}
			else
			{
				if (Code.ToLowerInvariant().Contains(text))
					return true;
				if (Name.ToLowerInvariant().Contains(text))
					return true;
			}
			return false;
		}
	}

	public class WiiGameListDiv : Div
	{
	}

	public class WiiGameTable : DataTable<WiiGameInfo>
	{

		private static HashSet<string> RegionFR = new HashSet<string>();
		private static HashSet<string> RegionUS = new HashSet<string>();

		static WiiGameTable()
		{
			RegionFR.Add("R57FMR");

			RegionUS.Add("RX3E01");
			RegionUS.Add("S3FE69");
			RegionUS.Add("RMLEH4");
			RegionUS.Add("RIPEAF");
			RegionUS.Add("SB4E01");
			RegionUS.Add("STKE08");
			RegionUS.Add("RHOE8P");
			RegionUS.Add("RTWE5D");
			RegionUS.Add("RH8E4F");
			RegionUS.Add("SVPESZ");
			RegionUS.Add("RDBE70");
			RegionUS.Add("R7BE20");

		}

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			base.Load(node, loader);
			if (Id == null)
				Id = "wiiGameList";
			AddColumn(null, 20, delegate(WiiGameInfo value, Column column, int index) { return OneControl(new Input(Enums.HtmlInputType.Checkbox, "games", value.Code)); }, null);
			AddColumn("Code", 70, delegate(WiiGameInfo value, Column column, int index) { return ParseText(value.Code); }, null);
			AddColumn("Cover", 80, delegate(WiiGameInfo value, Column column, int index) { return OneControl(new Img(value.Code, "http://art.gametdb.com/wii/cover/" + value.Region + "/" + value.Code + ".png")); }, null);
			AddColumn("Name", 200, delegate(WiiGameInfo value, Column column, int index) { return OneControl(new A(value.Name, "http://www.gametdb.com/Wii/" + value.Code) { Target = "_blank" }); }, null);
			AddColumn("Length", 50, delegate(WiiGameInfo value, Column column, int index) { return ParseText(value.Length.ToString("0.00")); }, null);
		}

		public override void ProcessCssRequest(HttpHandlerContext context)
		{
			context.Css.NewElement("#" + Id + " img").AddProperty("width", "80px").AddProperty("height", "112px");
			base.ProcessCssRequest(context);
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			SearchForm.SearchCommandHandler searchHandler = context.HandlerManager.GetHandler<SearchForm.SearchCommandHandler>();
			searchHandler.Search += new SearchForm.OnSearch(Search);
			base.ProcessJsonRequest(context);
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			SearchForm.SearchCommandHandler searchHandler = context.HandlerManager.GetHandler<SearchForm.SearchCommandHandler>();
			searchHandler.Search += new SearchForm.OnSearch(Search);
			using (CsvStream csv = new CsvStream(context.MapPath("/Users/jonv11/GameList.csv")))
			{
				// read headers
				string[] lines = csv.ReadCsvLine();
				AddHeaders();
				lines = csv.ReadCsvLine();
				while (lines != null)
				{
					lines = csv.ParseCsvLine(lines[0]);
					WiiGameInfo gameInfo = new WiiGameInfo();
					gameInfo.Code = lines[0];
					gameInfo.Name = lines[2];
					gameInfo.Length = double.Parse(lines[1], CultureInfo.InvariantCulture);
					if (RegionFR.Contains(gameInfo.Code))
						gameInfo.Region = "FR";
					else if (RegionUS.Contains(gameInfo.Code))
						gameInfo.Region = "US";
					AddTr(gameInfo);
					lines = csv.ReadCsvLine();
				}
			}
			base.ProcessHtmlRequest(context);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			if (!context.Js.Contains(Id))
				context.Js[Id].AppendAjaxHtmlHandler(this, true, false);
			base.ProcessJsRequest(context);
		}

		void Search(HttpHandlerContext context, SearchForm.SearchValues search)
		{
			string text = search.Term;
			Regex regex;
			try { regex = new Regex(text, RegexOptions.IgnoreCase); }
			catch { regex = null; }
			text = text.ToLowerInvariant();
			using (CsvStream csv = new CsvStream(context.MapPath("/Users/jonv11/GameList.csv")))
			{
				// read headers
				string[] lines = csv.ReadCsvLine();
				AddHeaders();
				lines = csv.ReadCsvLine();
				while (lines != null)
				{
					lines = csv.ParseCsvLine(lines[0]);
					WiiGameInfo gameInfo = new WiiGameInfo();
					gameInfo.Code = lines[0];
					gameInfo.Name = lines[2];
					gameInfo.Length = double.Parse(lines[1], CultureInfo.InvariantCulture);
					if (gameInfo.Match(regex, text))
					{
						if (RegionFR.Contains(gameInfo.Code))
							gameInfo.Region = "FR";
						else if (RegionUS.Contains(gameInfo.Code))
							gameInfo.Region = "US";
						Tr tr = AddTr(gameInfo);
						if (regex != null)
							tr.MarkText(regex, context.Translator);
					}
					lines = csv.ReadCsvLine();
				}
			}
			if (context.Json != null)
			{
				JsonObject table = new JsonObject();
				table.Add("seqNo", search.SeqNo);
				table.Add("html", ToInnerHtml(context.Translator));
				context.Json.Add(Id, table);
			}
		}
	}
}
